package org.lx.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.lx.common.core.page.TableDataInfo;
import org.lx.common.utils.UserThreadLocal;
import org.lx.nursing.domain.Reservation;
import org.lx.nursing.dto.ReservationPageDto;
import org.lx.nursing.mapper.ReservationMapper;
import org.lx.nursing.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation>  implements ReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public Integer getCancelledCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.getCancelledCount(startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> getCountByTime(Long time) {
        //将time时间戳转换为LocalDateTime
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay().plusHours(6);
        LocalDateTime endTime = startTime.plusHours(18);
         return reservationMapper.getCountByTime(startTime,endTime);
    }

    @Override
    public boolean insertReservation(Reservation reservation) {
        reservation.setCreateTime(LocalDateTime.now());
        reservation.setUpdateTime(LocalDateTime.now());
        reservation.setStatus(0);
        return save(reservation);
    }

    @Override
    public List<Reservation> getReservationPage(ReservationPageDto reservationDto) {
         PageHelper.startPage(reservationDto.getPageNum(),reservationDto.getPageSize());
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
        if (reservationDto.getStatus() != null) {
            queryWrapper.eq("status", reservationDto.getStatus());
        }
        return reservationMapper.selectList(queryWrapper);
    }

    @Override
    public boolean cancelReservation(Long id) {
       return reservationMapper.cancelReservation(id);
    }

    @Override
    public void updateReservationStatus() {
        //1.查询状态=0的以及预约时间小于当前时间的预约，全部更新为1
        LambdaQueryWrapper<Reservation> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(Reservation::getStatus,0);
        queryWrapper.le(Reservation::getTime, LocalDateTime.now());
        List<Reservation> reservationList = list(queryWrapper);
        if (CollectionUtils.isEmpty(reservationList)) {
            return;
        }
        UpdateWrapper<Reservation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", reservationList.stream().map(Reservation::getId).collect(Collectors.toList()))
                .set("status", 3)
                .set("update_time", LocalDateTime.now())
                .set("update_by", UserThreadLocal.getUserId());
        update(updateWrapper);
    }


}
